<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="开课学年" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
                <a-select
                  show-search
                  allowClear
                  placeholder="请选择开课学年"
                  v-model="queryParam.schoolYearId"
                  @change="changeSchoolYear">
                  <a-select-option v-for="item of this.schoolYearList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="开课学期" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
                <a-select show-search allowClear placeholder="请选择开课学期" v-model="queryParam.termId">
                  <a-select-option v-for="item of this.schoolTermList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="开课课程" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
                <a-select show-search allowClear placeholder="请选择开课课程" v-model="queryParam.courseId">
                  <a-select-option v-for="item of courseList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button
                  type="primary"
                  @click="$refs.table.refresh(true)"
                  :disabled="!queryParam.schoolYearId || !queryParam.termId || !queryParam.courseId"
                >查询</a-button
                >
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :showPagination="false"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.studentId"
      >
        <template class="table-operator" slot="operator">
          <x-down
            ref="batchExport"
            @batchExport="batchExport"
            :disabled="!queryParam.schoolYearId || !queryParam.termId || !queryParam.courseId"
          />
        </template>

        <span v-for="item in scopedSlotNames" :key="item.columnName" :slot="item.columnName" slot-scope="text, record">
          <template>
            <!-- {{item.projectId}}
            {{record[`course-selection-id-${item.projectId}`]}} -->
            <a href="#" @click="$refs.score.open(record[`course-selection-id-${item.projectId}`], 0)">{{
              record[item.columnName]
            }}</a>
          </template>
        </span>

        <span slot="defendTotalScore" slot-scope="text, record">
          <template>
            <a href="#" @click="$refs.score.open(record.defendTotalScoreLatestCourseSelectionId, 1)">{{ record.defendTotalScore }}</a>
          </template>
        </span>
      </s-table>
    </a-card>
    <score ref="score" />
  </div>
</template>
<script>
import { STable, XDown } from '@/components'
import { queryScoreStatistics, scoreStatisticsExport } from '@/api/modular/main/courseHomeworkManage'
import { sysSchoolYearList } from '@/api/modular/system/sysSchoolYearManage'
import { sysSchoolTermList } from '@/api/modular/system/sysSchoolTermManage'
import { courseList } from '@/api/modular/system/courseManage'
import score from './score.vue'
export default {
  components: {
    STable,
    XDown,
    score
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // 查询参数
      queryParam: {
        schoolYearId: undefined,
        termId: undefined,
        courseId: undefined
      },
      swtichLoading: false,
      // 表头
      columns: [
        // {
        //   title: '名称',
        //   align: 'center',
        //   dataIndex: 'name',
        // }
      ],
      scopedSlotNames: [], // 动态自定义表头，分数部分
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return queryScoreStatistics(Object.assign(parameter, this.queryParam)).then((res) => {
          this.columns = res.data.tableHeaderList
            .filter((item) => item.labelName != null)
            .map((item) => {
              let result = {
                title: item.labelName,
                align: 'center',
                dataIndex: item.columnName
              }
              // 如果是分数，自定义列
              if (item.columnName.indexOf('project-score-') != -1 || item.columnName == 'defendTotalScore') {
                result = {
                  ...result,
                  scopedSlots: { customRender: item.columnName }
                }
              }
              // 如果是项目分数，是动态列
              if (item.columnName.indexOf('project-score-') != -1) {
                this.scopedSlotNames.push({
                  projectId: item.columnName.substring(14),
                  columnName: item.columnName
                })
              }
              return result
            })
          return res.data.tableDataList
        })
      },
      courseList: [],
      schoolYearList: [],
      schoolTermList: []
    }
  },
  created() {
    this.getSchoolYearList()
    this.getSchoolTermList()
    this.getCourseList()
  },
  methods: {
    /**
     * load学年列表
     */
    getSchoolYearList() {
      sysSchoolYearList().then((res) => {
        if (res.success) {
          this.schoolYearList = res.data
        }
      })
    },
    /**
     * load开课学期
     */
    getSchoolTermList(schoolYearId) {
      sysSchoolTermList({
        schoolYearId: schoolYearId
      }).then((res) => {
        if (res.success) {
          this.schoolTermList = res.data
        }
      })
    },
    /**
     * load课程
     */
    getCourseList() {
      courseList().then((res) => {
        if (res.success) {
          this.courseList = res.data
        }
      })
    },
    /**
     * 批量导出
     */
    batchExport() {
      scoreStatisticsExport(this.queryParam).then((res) => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    changeSchoolYear(schoolYearId) {
      this.queryParam.termId = undefined
      this.getSchoolTermList(schoolYearId)
    }
  }
}
</script>
<style lang="less">
</style>
